//
//  SubmitOrderViewController.m
//  BP_NewClient
//
//  Created by  on 15/3/9.
//  Copyright (c) 2015年 normal. All rights reserved.
//

#import "AppDelegate.h"

#import "SubmitOrderViewController.h"
#import "ConfirmOrderViewController.h"
#import "PlusCutView.h"
#import "SubmitOrderViewCell.h"
#import "BURequestManager.h"
#import "CouponListViewController.h"
#import <AlipaySDK/AlipaySDK.h>

@interface SubmitOrderViewController ()

@property (nonatomic, strong) NSString *dateString;
@property (nonatomic, strong) NSString *payMethod;
@property (nonatomic, strong) NSString *coupont_id;

@end

@implementation SubmitOrderViewController

- (id)init{
    if (self = [super init]) {
        self.dateString = [NSString getYearMonthDay:[NSDate date]];
        self.payMethod = @"200"; //默认 － 支付宝
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(payResultCallBack:) name:ALIXPAY_RESULT_NOTIFICATION object:nil];
    }
    return self;
}

// delegate+pay 支付回调
- (void) payResultCallBack:(NSNotification*) notification
{
    ConfirmOrderViewController *confrim = [[ConfirmOrderViewController alloc] init];
    [self.navigationController pushViewController:confrim animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"提交订单";
    self.navigationItem.leftBarButtonItem = BACK_BAR_BUTTON_BACK(@selector(back:));
    self.view.backgroundColor = [UIColor whiteColor];

    orderTable.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight - KNavBarHeight - 20 - 60);
    
    footView.backgroundColor = [BPCommon colorWithHexValue:0xf8f8f8 alpha:1.0f];
    footView.frame = CGRectMake(0, orderTable.frame.size.height, ScreenWidth, 60);
    
    NSDictionary *dic = [NSDictionary dictionaryWithObject:self.productModel.id forKey:@"product_id"];
    
<<<<<<< HEAD
    [[BURequestManager sharedRequest] requestGetWithParameterWithBlockDic:ORDER_SUBMIT_INFO SetParamer:dic success:^(NSDictionary *dic){
//        
=======
    [[BURequestManager sharedRequest] requestGetWithParameterWithBlockDic:ORDER_SUBMIT_INFO SetParamer:nil success:^(NSDictionary *dic){
        
>>>>>>> ceba347648d240cfcb94a979ed67bf23465e238d
        self.orderModel = [[OrderModel alloc] initWithDictionary:[dic objectForKey:@"data"] error:nil];
        amoutPrice.text = [NSString safeString:self.orderModel.prize];
        [orderTable reloadData];

<<<<<<< HEAD
        totalPrice.text = [NSString stringWithFormat:@"¥ %@",self.orderModel.price];
        //
//        [bigImage setImageWithURL:[NSURL URLWithString:self.productModel.storeImg]
//                 placeholderImage:[UIImage imageNamed:@"2.jpg"]];
//        
//        locationAddress.text = self.productModel.address;
//        storeName.text = self.productModel.name;
//        
//        [self._tableView reloadData];
        
=======
>>>>>>> ceba347648d240cfcb94a979ed67bf23465e238d
    } failure:^(NSString *errMsg){
        
        [self showNoticeMsg:errMsg];
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        return 50;
    }else if (indexPath.row == 1){
        return 50.f;
    }
    else if(indexPath.row == 2){
        return 50.f;

    } else if(indexPath.row == 3){
        return 50.f;
    }
    else{
        return 131;
    }
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identy = @"SubmitOrderViewCell";
    SubmitOrderViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:identy];
    
    NSArray *nibs;
    if (nil == cell) {
        nibs = [[NSBundle mainBundle]loadNibNamed:@"SubmitOrderViewCell" owner:nil options:nil];
    }
    
    cell.contentView.backgroundColor = [UIColor blueColor];
    if (indexPath.row == 0) {
        cell = [nibs objectAtIndex:0];
        if (self.orderModel.price) {
            cell.priceLabel.text = [NSString stringWithFormat:@"%@元",self.orderModel.price];
        }
        
    }else if (indexPath.row == 1){
        
        cell = [nibs objectAtIndex:1];
    }else if (indexPath.row == 2){
        cell = [nibs objectAtIndex:3];

    }else if (indexPath.row == 3){
        
        cell = [nibs objectAtIndex:4];

    }else{
        cell = [nibs objectAtIndex:2];
        cell.payMethod_Block = ^(NSString *strPay){
            self.payMethod = strPay;
        };
    }
//    cell.contentView.backgroundColor = [BPCommon colorWithHexValue:0xeeeeee alpha:1.0f];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    if (indexPath.row == 1) {
        CouponListViewController *couponList = [[CouponListViewController alloc] init];
        couponList.orderModel = self.orderModel;
        couponList.couponBlock = ^(CouponModel *model){
            self.coupont_id = model.id;
        };
        [self.navigationController pushViewController:couponList animated:YES];
        
    }else if (indexPath.row == 3) {

        if (!chvc) {
            chvc = [[CalendarHomeViewController alloc]init];
            chvc.navigationItem.leftBarButtonItem = BACK_BAR_BUTTON_BACK(@selector(back:));
            chvc.calendartitle = @"选择日期";
            [chvc setAirPlaneToDay:365 ToDateforString:nil];//飞机初始化方法
        }
        
        __weak SubmitOrderViewController *weakSelf = self;
        
        chvc.calendarblock = ^(CalendarDayModel *model){
            /*
            NSLog(@"\n---------------------------");
            NSLog(@"1星期 %@",[model getWeek]);
            NSLog(@"2字符串 %@",[model toString]);
            NSLog(@"3节日  %@",model.holiday);
            */
            
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            UILabel *day = (UILabel *)[cell.contentView viewWithTag:1000];
            
                day.text = [model toString];
                weakSelf.dateString = day.text;
        };
        
        [self.navigationController pushViewController:chvc animated:YES];
    }

}

- (IBAction)confirmOrder:(id)sender{
    
    // 日期 优惠券 id
    if ([NSString isBlankString:self.productModel.id]) {
        [self showNoticeMsg:@"产品异常"];
        return;
    }else if ([NSString isBlankString:self.dateString]){
        [self showNoticeMsg:@"请选择到店日期"];
        return;
    }else if ([NSString isBlankString:self.payMethod]){
        [self showNoticeMsg:@"请选择支付方式"];
        return;
    }
    
    
    NSDictionary *paramer = @{@"user_id":@"1",@"product_id":self.productModel.id,@"product_num":@"1",@"pay_method":[NSString safeString:self.payMethod],@"amount":[NSString safeString:amoutPrice.text],@"date":[NSString getIntervalFromDateStr:self.dateString],@"coupon_id":[NSString safeString:self.coupont_id],@"remark":@""};
    
<<<<<<< HEAD
    [[BURequestManager  sharedRequest] requestPostWithParameterWithBlockDic:@"orders/submit_order" SetParamer:@{@"user_id":@"1",@"product_id":@"1",@"product_num":@"1",@"pay_method":@"200",@"remark":@"12",@"amount":@"1500"} success:^(NSDictionary *duc) {
=======
    [[BURequestManager  sharedRequest] requestPostWithParameterWithBlockDic:SUBMIT_ORDER SetParamer:paramer success:^(NSDictionary *duc) {
>>>>>>> ceba347648d240cfcb94a979ed67bf23465e238d
        NSLog(@"duc is %@",duc)
        ;
        NSString *orderString =[[duc objectForKey:@"data"] objectForKey:@"pay_str"];
        
        [[AlipaySDK defaultService] payOrder:orderString fromScheme:SCHEME callback:^(NSDictionary *resultDic) {

            NSString *status = [resultDic objectForKey:@"resultStatus"];
            if ([status integerValue] == 9000) {
                [[NSNotificationCenter defaultCenter] postNotificationName:ALIXPAY_RESULT_NOTIFICATION object:resultDic];
            }

        }];
        
    }failure:^(NSString *errorMsg){
        [self showNoticeMsg:errorMsg];
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
